#pragma once

#include "quote_manager.h"
#include "websocket_server.h"
#include "market_data_simulator.h"
#include "config_manager.h"
#include <memory>
#include <atomic>

namespace yquote {

class QuoteServer {
public:
    QuoteServer(int port = 8080, const std::string& config_file = "config/server.conf");
    ~QuoteServer();
    
    // 启动服务器
    bool start();
    
    // 停止服务器
    void stop();
    
    // 检查是否正在运行
    bool isRunning() const;
    
    // 获取组件引用
    QuoteManager& getQuoteManager() { return *quote_manager_; }
    WebSocketServer& getWebSocketServer() { return *websocket_server_; }
    MarketDataSimulator& getMarketDataSimulator() { return *market_simulator_; }
    ConfigManager& getConfigManager() { return *config_manager_; }
    
    // 打印统计信息
    void printStatistics() const;
    
private:
    std::unique_ptr<ConfigManager> config_manager_;
    std::unique_ptr<QuoteManager> quote_manager_;
    std::unique_ptr<WebSocketServer> websocket_server_;
    std::unique_ptr<MarketDataSimulator> market_simulator_;
    
    std::atomic<bool> running_;
    
    // 回调函数
    void onQuoteUpdate(const QuoteData& quote);
    void onClientMessage(const std::string& client_id, const std::string& message);
    void onClientConnect(const std::string& client_id);
    void onClientDisconnect(const std::string& client_id);
    
    // 配置相关
    void loadConfiguration(const std::string& config_file);
    void applyConfiguration();
    
    // 消息处理
    void processSubscribeMessage(const std::string& client_id, const std::string& message);
    void processUnsubscribeMessage(const std::string& client_id, const std::string& message);
    void processPingMessage(const std::string& client_id);
    void sendErrorResponse(const std::string& client_id, const std::string& error_code, const std::string& error_message);
};

} // namespace yquote 